package me.zhanshi123.pixelpaygui.gui.paymoney

import me.zhanshi123.pixelpaygui.gui.open.OpenGuiHolder
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryClickEvent

class PayGuiListener : Listener {
    @EventHandler(ignoreCancelled = true)
    fun onClick(e: InventoryClickEvent) {
        val inventory = e.clickedInventory ?: return
        val player = e.whoClicked as Player
        if (player.openInventory.topInventory.holder !is PayGuiHolder) {
            return
        }
        e.isCancelled = true
        if (inventory.holder !is PayGuiHolder) {
            return
        }
        val holder = inventory.holder as PayGuiHolder
        val slot = e.slot
        if (slot == 40) {
            player.closeInventory()
            return
        }

        val type = slot.let {
            when (it) {
                20 -> {
                    "a"
                }
                22 -> {
                    "w"
                }
                24 -> {
                    "q"
                }
                else -> {
                    null
                }
            }
        } ?: return
        player.closeInventory()
        player.performCommand("vexrmb create ${holder.money} $type")
    }
}